package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.CdzGiftMapper;
import com.zhentao.mapper.CdzWinningRecordingMapper;
import com.zhentao.mapper.UsersMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.users.CdzGift;
import com.zhentao.model.common.pojo.users.CdzWinningRecording;
import com.zhentao.model.common.pojo.users.Users;
import com.zhentao.service.ICdzWinningRecordingService;
import com.zhentao.userLocal.UserThreadLocalUtil;
import com.zhentao.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 中奖记录表 服务实现类
 * </p>
 *
 * @author mp
 * @since 2024-12-27
 */
@Service
public class CdzWinningRecordingServiceImpl extends ServiceImpl<CdzWinningRecordingMapper, CdzWinningRecording> implements ICdzWinningRecordingService {
    @Autowired
    private CdzGiftMapper cdzGiftMapper;
    @Resource
    private UsersMapper usersMapper;
    @Override
    public ResponseResult add(Integer gitfId) {

//        获取用户id
        Long userId = UserThreadLocalUtil.getUser();

        Users users = usersMapper.selectOne(Wrappers.<Users>lambdaQuery().eq(Users::getId, userId));
//        获取用户积分
        int point = Integer.parseInt(users.getPoint());
//        判断用户积分
        if (point < 50) {
            return ResponseResult.errorResult(400,"积分不足");
        }

        //        减去用户积分
        users.setPoint(String.valueOf(point-50));
//        修改
        usersMapper.updateById(users);
        //        执行查询
        CdzGift cdzGift = cdzGiftMapper.selectOne(Wrappers.<CdzGift>lambdaQuery().eq(CdzGift::getId,gitfId));
        if (cdzGift == null) {
            return ResponseResult.errorResult(500,"奖品不存在");
        }
        //        未中奖则不添加
        if (cdzGift.getName().equals("未中奖")){
            return ResponseResult.errorResult(500,"未中奖");
        }

        String point1 = users.getPoint();
//        新建实体类
        CdzWinningRecording cdzWinningRecording = new CdzWinningRecording();
        cdzWinningRecording.setGitfId(gitfId);
        cdzWinningRecording.setUserId(userId);
        cdzWinningRecording.setStatus(0);
        cdzWinningRecording.setGiftNumber(new SnowflakeIdGenerator().nextId());
        if (save(cdzWinningRecording)){
            return ResponseResult.okResult(cdzWinningRecording);
        }
        return ResponseResult.okResult(500,"添加失败");
    }

    @Override
    public ResponseResult updateStatus(Long giftNumber) {
        CdzWinningRecording winningRecording = this.getOne(Wrappers.<CdzWinningRecording>lambdaQuery().eq(CdzWinningRecording::getGiftNumber, giftNumber));
        Integer status = winningRecording.getStatus();
        if (status == 1){
            return ResponseResult.errorResult(500,"已兑换");
        }

        if (status == 0){
            winningRecording.setStatus(1);
            this.updateById(winningRecording);
            return ResponseResult.okResult("修改成功");
        }
        return ResponseResult.okResult(winningRecording);
    }
}
